package com.liuyi.netty.im.lychat.modular.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuyi.netty.im.lychat.LychatConst;
import com.liuyi.netty.im.lychat.modular.caches.GroupCache;
import com.liuyi.netty.im.lychat.modular.entity.Group;
import com.liuyi.netty.im.lychat.modular.mapper.GroupMapper;
import com.liuyi.netty.im.lychat.modular.service.IGroupMemberService;
import com.liuyi.netty.im.lychat.modular.service.IGroupService;
import com.liuyi.rapid.develop.tool.unusual.BizException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 群组表 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-12-29
 */
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements IGroupService {

    @Resource
    private GroupCache cache;
    @Resource
    private IGroupMemberService groupMemberService;


    @Override
    public GroupCache cache() {
        return cache;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void dissolveGroup(String userId, String groupId) {
        Group group = this.getById(groupId);
        if (group == null) {
            throw new BizException("群聊不存在，请刷新后重试");
        }
        if (!group.getGroupManage().equals(userId)) {
            throw new BizException("抱歉，您不是群组，无权限进行该操作");
        }
        //删除群聊
        if (!this.removeById(groupId)) {
            throw new BizException("解散群聊失败，请是稍后重试。");
        }
        cache.removeById(groupId);
        //解散群成员
        groupMemberService.dissolveGroup(groupId);

    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public String createGroup(String groupName, String groupManage, List<String> memberIds) {
        String groupNo = primaryGenerate(() -> RandomUtil.randomNumbers(8), Group.GROUP_NO);
        Group group = new Group().setGroupNo(groupNo).setGroupName(groupName)
                .setGroupManage(groupManage).setGroupHead(LychatConst.DEFAULT_GROUP_HEAD);
        this.save(group);
        groupMemberService.createGroup(groupNo, groupManage, memberIds);
        return groupNo;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public String createGroup(String groupName, String groupManage) {
        String groupNo = primaryGenerate(() -> RandomUtil.randomNumbers(8), Group.GROUP_NO);
        createGroup(groupNo, groupName, groupManage);
        return groupNo;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void createGroup(String groupNo, String groupName, String groupManage) {
        Group group = new Group().setGroupNo(groupNo).setGroupName(groupName)
                .setGroupManage(groupManage).setGroupHead(LychatConst.DEFAULT_GROUP_HEAD);
        this.save(group);
        groupMemberService.createGroup(groupNo, groupManage);
    }
}
